<template>
  <div style="width: 100%" v-if="visible">
    <el-form ref="form" :model="form" :rules="rules" label-position="top" size="mini">
      <el-row>
        <el-col :span="24">
          <el-form-item label="机器" prop="machineId">
            <el-select v-model="form.machineId" placeholder="请选择机器" clearable :style="{width: '100%'}">
              <el-option v-for="(item, index) in machineList" :key="index" :label="item.label" :value="item.value"
                         :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="构建命令" prop="buildScript">
            <div style="width: 100%;overflow-x: scroll">
              <AceEditor
                v-model="form.buildScript"
                width="955px"
                height="150px"
                :fontSize="14"
                :showPrintMargin="true"
                :showGutter="true"
                :highlightActiveLine="true"
                mode="sh"
                theme="monokai"
                :onChange="onChange"
                name="editor"
                :editorProps="{$blockScrolling: true}"
                :wrapEnabled="true"
              />
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" style="width: 100%" type="primary" @click="submitForm">保 存</el-button>
    </div>
  </div>
</template>

<script>
  // see https://www.npmjs.com/package/vue2-brace-editor
  import {Ace as AceEditor} from 'vue2-brace-editor';
  import {nodeInfo, updateInfo, addNodeInfo} from '@/lib/api-pipeline';

  import 'brace/mode/sh';
  import 'brace/theme/monokai';

  import Vue from 'vue'
  import Element from 'element-ui'
  import 'element-ui/lib/theme-chalk/index.css';

  Vue.use(Element)

  export default {
    name: "diy-template",
    components: {
      AceEditor
    },
    data() {
      return {
        id:0,
        visible: false,
        nodeUuid: '',
        initBuildScript: '',
        form: {
          buildScript: this.initBuildScript
        },
        radio: 1,
        machineList: [],
        rules: {
          machineId: [{required: true, message: '机器不能为空', trigger: 'blur'}],
          buildScript: [{required: true, message: '构建命令不能为空', trigger: 'blur'}],
        }
      }
    },
    methods: {
      init(nodeUuid) {
        //组件数据初始化，防止出现使用同一个组件的两个节点之间数据关联
        Object.assign(this.$data, this.$options.data())
        this.form = {
          buildScript: this.initBuildScript
        }
        this.nodeUuid = nodeUuid
        nodeInfo({'nodeUuid': nodeUuid}).then(response => {
          if (response.code === 200) {
            const data = response.data
            if (data) {
              this.form = JSON.parse(data.info)
              this.id = data.id
            }
          } else {
            this.msgError(response.msg)
          }
          this.visible = true
        })
        this.$api.getMachineList({
          page: 1,
          limit: 100
        }).then(({ data }) => {
          this.machineList = data.value.map(item => {
            return {
              value: item.id,
              label: item.name
            }
          })
        }).catch(() => {
          console.log('err')
        })
      },
      //获取当前节点的配置文件（改之前的每个配置节点提交为统一流水线一起提交到后台）
      getNodeConfig(){
        return this.form
      },
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            // 构建数据json
            let info = {
              'id':this.id,
              'nodeUuid': this.nodeUuid,
              'nodeName': '空模板',
              'info': JSON.stringify(this.form)
            }
            if (this.id !== 0) {
              updateInfo({'pipelineNodeInfoDTO': info}).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('修改成功')
                } else {
                  this.msgError(response.msg)
                }
              })
            } else {
              addNodeInfo({'pipelineNodeInfoDTO': info}).then(response => {
                if (response.code === 200) {
                  this.id = response.data //更新新增节点id
                  this.msgSuccess('新增成功')
                } else {
                  this.msgError(response.msg)
                }
              })
            }
          }
        })
      },
      onChange(data){
        this.form.buildScript = data
      }
    }
  }
</script>

<style scoped>

</style>
